﻿function InitList() {
    BindCity();
    parkMSys.TryFuncByCondition(function () {
        return $("#ddl_City").val() != null;
    }, function () {
        Search();
        $("#btnAdd").click(function () {
            Add();
        });
    });
    $("#chkAll").click(function () {
        var me = $(this);
        if (me.is(":checked")) {
            $(":checkbox[myid]").attr("checked", "checked");
        }
        else {
            $(":checkbox[myid]").removeAttr("checked");
        }
    });
}

function ajax(method, param, success) {
    $.ajax({
        url: "../Led/List.aspx?m=" + method + "&rnd=" + Math.random()
        , dataType: "json"
        , type: "post"
        , data: param
        , success: function (data) {
            if (!AjaxError(data)) {
                success(data);
            }
        }
    });
};

function Search() {
    var cid = $("#ddl_City").val();
    var aid = $("#ddl_Area").val();
    var sid = $("#ddl_Street").val();
    var code = encodeURIComponent($("#txtName").val());
    ajax("search", { cid: cid, aid: aid, sid: sid, code: code }, function (json) {
        var html = $("#full_CollectorList").tmpl(json.data);
        $("#tbl_CollectorList").find("tr:not(.td_title)").remove();
        $("#tbl_CollectorList").append(html);
    });
}

function Add() {
    location.replace("../Led/Add.aspx");
}

function Edit(id) {
    location.replace("../Led/Edit.aspx?id="+id);
}

function Delete(id) {
    if (confirm("确实要删除该诱导屏吗？")) {
        ajax("delete", { id: id }, function (json) {
            if (json.result-0 == 1) {
                $("tr[flag=" + id + "]").remove();
            }
            else {
                alert("删除失败");
            }
        });
    }
}

function Location(id) {
    ajax("getinfo", { id: id }, function (json) {
        var html1 = {};
        var s = $("#locationTmpl").tmpl(json);
        html1.html = s[0].outerHTML;
        ajax("getledlocation", { id: id }, function (json2) {            
            html1.lng = json2.lng - 0;
            html1.lat = json2.lat - 0;
            MapLocation([html1]);
        });
    });   
}

function InitAdd() {
    $("#installTime").datepicker();
    ajax("GetLedParentInfo", { cid: "", aid: "", sid: "" }, function (json) {        
        $("#parentId").append("#tempparentId", json.data); 
    });
    $("#btnSave").click(function () {
        var param = {};
        param.code = encodeURIComponent($("#code").val());
        param.name = encodeURIComponent($("#name").val());
        param.ledCode = encodeURIComponent($("#ledCode").val());
        param.parentId = encodeURIComponent($("#parentId").val());
        param.enable = encodeURIComponent($("#enable").is(":checked") ? 1 : 0);
        param.installTime = encodeURIComponent($("#installTime").val());
        param.defaultText = encodeURIComponent($("#defaultText").val());

        ajax("add", param, function (r) {            
            if (r.result - 0 == 1) {
                alert("添加成功！");
                window.location.replace("../Led/List.aspx");
            }
            else {
                alert("添加失败！");
            }
        });
    });
}

function InitEdit() {
    var id = QueryString("id");
    ajax("getinfo", { id: id }, function (r) {
        $("#name").val(r.name);
        
        if (r.enable - 0 == 1) {
            $("#enable").attr("checked", "checked");
        }
        $("#defaultText").val(r.defaultText);

        ajax("GetLedParentInfo", { cid: "", aid: "", sid: "" }, function (json) {
            $("#parentId").append("#tempparentId", json.data);
            $("#parentId").val(r.parentId);
        });

        $("#btnSave").click(function () {
            var param = {};
            param.id = encodeURIComponent(id);
            param.name = encodeURIComponent($("#name").val());
            param.parentId = encodeURIComponent($("#parentId").val());
            param.enable = encodeURIComponent($("#enable").is(":checked") ? 1 : 0);
            param.defaultText = encodeURIComponent($("#defaultText").val());

            ajax("edit", param, function (r) {
                if (r.result - 0 == 1) {
                    alert("修改成功！");
                    window.location.replace("../Led/List.aspx");
                }
                else {
                    alert("修改失败！");
                }
            });
        });
    });
}

function InitSetLed() {
    $("#ids").val(QueryString("ids"));
    $("#btnSave").click(function () {
        var entity = {};
        entity.ledBrightness = $("#selBright").val();
        var jsonentity = encodeURIComponent( JSON.stringify(entity));
        ajax("setled", { ids: $("#ids").val(), entity: jsonentity }, function (r) {
            if (r.result - 0 == 1) {
                alert("设置成功！");
            }
            else {
                alert("设置失败！");
            }
        });
    });
}

function Set(ids) {
    window.location.replace("setledparam.aspx?ids=" + ids);
}

function SetMany() {
    var chks = $(":checkbox:checked[myid]");
    if (chks.size() == 0) {
        alert("请选择至少一个要设置的诱导屏");
        return;
    }

    var ids = "";
    $.each(chks, function () {
        ids += $(this).attr("myid") + ",";
    });
    ids = ids.replace(/,$/, ""); 
    Set(ids);
}

function InitInsertText() {
    $("#ids").val(QueryString("ids"));
    $("#start,#end").datetimepicker({ dateFormat: "yy-mm-dd" });
    $("#btnSave").click(function () {
        var entity = {};
        entity.text = $("#text").val();
        entity.start = $("#start").val();
        entity.end = $("#end").val();
        entity.frequency = $("#frequency").val();
        var jsonentity = encodeURIComponent(JSON.stringify(entity));
        ajax("inserttext", { ids: $("#ids").val(), entity: jsonentity }, function (r) {
            if (r.result - 0 == 1) {
                alert("插播成功！");
            }
            else {
                alert("插播失败！");
            }
        });
    });
}

function InsertText(ids) {
    window.location.replace("InsertText.aspx?ids=" + ids);
}

function InsertMany() {
    var chks = $(":checkbox:checked[myid]");
    if (chks.size() == 0) {
        alert("请选择至少一个要插播的诱导屏");
        return;
    }

    var ids = "";
    $.each(chks, function () {
        ids += $(this).attr("myid") + ",";
    });
    ids = ids.replace(/,$/, "");
    InsertText(ids);
}